package u.can.i.up.gearmanimpl.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 *@author nancy
 *@version 2013-5-21上午9:05:36
 */
public class Analyzer {
	@SuppressWarnings("unchecked")
	public XmlResultData xmlAnalysis(String xmlPath){
		SAXBuilder saxBuilder = new SAXBuilder();
		XmlResultData xmlResultData= new XmlResultData();
	    List<ResultData> resultDatalist=new ArrayList<ResultData>();
	    		
		try {
			Document doc = saxBuilder.build(new File(xmlPath));
			Element ApkResults = doc.getRootElement();
			String ApkName = ApkResults.getChildText("ApkName");
			String ApkDescription=ApkResults.getChildText("ApkDescription");
			String MD5=ApkResults.getChildText("MD5");
			List<String> highlightList =new ArrayList<String>();
//			List<Element> string=ApkResults.getChild("highlightList").getChildren("String");
//			for (Element element : string) {
//				highlightList.add(element.getText());
//			}
			List<String> JFQInfor =new ArrayList<String>();
//			List<Element> JFQInforList=ApkResults.getChild("JFQInforList").getChildren();
//			for (Element element : JFQInforList) {
//				String JFQInfortext =element.getText();
//				JFQInfor.add(JFQInfortext);
//			}
			
			List<Element> ResultList = ApkResults.getChildren("Result");
			for(Element Result: ResultList){
				ResultData resultData=new ResultData();
				String Filename = Result.getChildText("Filename");
				String RiskLevel=Result.getChildText("RiskLevel");
				String LineNum =Result.getChildText("LineNum");
				String ColumnNum =Result.getChildText("ColumnNum");
				String APIName =Result.getChildText("APIName");
				String RuleId = Result.getChildText("RuleId");
				String Catagory=Result.getChildText("Catagory");
				String Discription =Result.getChildText("Discription");
				List<String> FlowPath =new ArrayList<String>();
				List<String> FlowPathAnalysis =new ArrayList<String>();
				List<Element> FlowPathList= Result.getChild("FlowPaths").getChildren("FlowPath");
				List<Element> FlowPathAnalysisList=Result.getChild("FlowPathAnalysises").getChildren("FlowPathAnalysis");
				for (Element element : FlowPathList) {
					FlowPath.add(element.getText());
					
				}
				for (Element element : FlowPathAnalysisList) {
					FlowPathAnalysis.add(element.getText());
				}
				String ModifyTime =Result.getChildText("ModifyTime");
				resultData.setFilename(Filename);
				resultData.setRiskLevel(Integer.valueOf(RiskLevel));
				resultData.setLineNum(Integer.valueOf(LineNum));
				resultData.setColumnNum(Integer.valueOf(ColumnNum));
				resultData.setAPIName(APIName);
				resultData.setRuleId(RuleId);
				resultData.setCatagory(Catagory);
				resultData.setDiscription(Discription);
				resultData.setModifyTime(ModifyTime);
				resultData.setFlowPath(FlowPath);
				resultData.setFlowPathAnalysis(FlowPathAnalysis);
				resultDatalist.add(resultData);
				
			}
		xmlResultData.setApkName(ApkName);
		xmlResultData.setApkDescription(ApkDescription);
	    xmlResultData.setMD5(MD5);
	    xmlResultData.setHighlightList(highlightList);
	    xmlResultData.setJFQList(JFQInfor);
	    xmlResultData.setRdList(resultDatalist);
		
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return xmlResultData;	
	}
	
	@SuppressWarnings("resource")
	public XmlResultData xmltxtAnalysis(String xmlPath,String txtPath){
		XmlResultData xmlResultData= new XmlResultData();
		xmlResultData=xmlAnalysis(xmlPath);
		 List<String> txtList = new ArrayList<String>();
			try {
				BufferedReader br  = new BufferedReader(new InputStreamReader(new FileInputStream(txtPath)));
				String line;
				while ((line=br.readLine())!=null) {
					if (line.isEmpty()||line.equals("http://schemas.android.com/apk/res/android")||line.endsWith(".apk")) {
						continue;
					}
					txtList.add(line.trim());
				
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			xmlResultData.setTxtList(txtList);
			return xmlResultData;
		
	}
}
